package sk.tuke.serviceImpl;

import javax.jws.WebMethod;
import javax.ws.rs.core.Response;

import sk.tuke.domain.Department;
import sk.tuke.domain.Insurance;
import sk.tuke.domain.IO.businessManagement.CreateDepartmentInput;
import sk.tuke.domain.IO.businessManagement.CreateInstituteInput;
import sk.tuke.domain.IO.businessManagement.CreateInsuranceInput;
import sk.tuke.domain.IO.businessManagement.CreateInsuranceOutput;
import sk.tuke.domain.IO.businessManagement.GetDepartmentInput;
import sk.tuke.domain.IO.businessManagement.GetDepartmentOutput;
import sk.tuke.domain.IO.businessManagement.GetInsuranceInput;
import sk.tuke.domain.IO.businessManagement.GetInsuranceOutput;
import sk.tuke.persistance.DepartmentPo;
import sk.tuke.persistance.InsurancePo;
import sk.tuke.serviceDao.AbstractDao;
import sk.tuke.serviceDao.BusinessManagementDao;
import sk.tuke.serviceInterface.BusinessManagementService;
import sk.tuke.utils.MapperDomainToPo;
import sk.tuke.utils.MapperPOtoDomain;
import sk.tuke.utils.Utils;

public class BusinessManagementServiceImpl extends CommonService implements BusinessManagementService {

	private BusinessManagementDao businessManagementDao;

	private MapperDomainToPo mapperDomainToPo;

	private MapperPOtoDomain mapperPoToDomain;

	private AbstractDao abstractDao;

	public MapperDomainToPo getMapperDomainToPo() {
		return mapperDomainToPo;
	}

	public void setMapperDomainToPo(MapperDomainToPo mapperDomainToPo) {
		this.mapperDomainToPo = mapperDomainToPo;
	}

	public MapperPOtoDomain getMapperPoToDomain() {
		return mapperPoToDomain;
	}

	public void setMapperPoToDomain(MapperPOtoDomain mapperPoToDomain) {
		this.mapperPoToDomain = mapperPoToDomain;
	}

	public Response CreateInstitute(CreateInstituteInput createInstituteInput) throws Throwable {

		return null;
	}

	public Response CreateInsurance(CreateInsuranceInput createInsuranceInput) throws Throwable {
		try {
			Utils.checkObjectToNull(new String[] { "createInsuranceInput" }, createInsuranceInput);
			Insurance insurance = createInsuranceInput.getInsurance();
			Utils.checkObjectToNull(new String[] { "insurance" }, insurance);

			InsurancePo insurancePo = businessManagementDao.createInsurance(insurance.getNumber(), insurance.getName(), insurance.getPhone(),
					insurance.getEmail(), insurance.getContactPerson(), insurance.getStreet(), insurance.getStreetNum(), insurance.getCity(),
					insurance.getZip(), insurance.getState());

			CreateInsuranceOutput result = new CreateInsuranceOutput();
			result.setInsurance(mapperPoToDomain.mapInsuranceToInsurancePo(insurancePo));
			return Response.status(Response.Status.OK).entity(result).build();
		} catch (Exception e) {
			return handleException(CreateInsuranceOutput.class, e, Response.Status.BAD_REQUEST);
		}

	}

	@WebMethod
	public Response getInsurance(GetInsuranceInput getInsuranceInput) throws Throwable {
		try {
			InsurancePo insurancePo = businessManagementDao.getInsurance(getInsuranceInput.getNumber());
			if (insurancePo == null) {
				throw new Exception("Any insurance with defined search parameters does not exists in the database.");
			}
			Insurance insurance = mapperPoToDomain.mapInsuranceToInsurancePo(insurancePo);
			GetInsuranceOutput result = new GetInsuranceOutput();
			result.setInsurance(insurance);
			return Response.status(Response.Status.OK).entity(result).build();
		} catch (Exception e) {
			return handleException(GetInsuranceOutput.class, e, Response.Status.BAD_REQUEST);
		}
	}

	public Response CreateDepartment(CreateDepartmentInput createDepartmentInput) throws Throwable {
		// TODO Auto-generated method stub
		return null;
	}

	public BusinessManagementDao getBusinessManagementDao() {
		return businessManagementDao;
	}

	public void setBusinessManagementDao(BusinessManagementDao businessManagementDao) {
		this.businessManagementDao = businessManagementDao;
	}

	public AbstractDao getAbstractDao() {
		return abstractDao;
	}

	public void setAbstractDao(AbstractDao abstractDao) {
		this.abstractDao = abstractDao;
	}

	@WebMethod
	public Response getDepartment(GetDepartmentInput getDepartmentInput) throws Throwable {
		try {
			Utils.checkObjectToNull(new String[] { "getDepartmentInput" }, getDepartmentInput);
			DepartmentPo departmentPo = businessManagementDao.getDepartment(getDepartmentInput.getNumber());
			if (departmentPo == null) {
				throw new Exception("Any department with defined search parameters does not exists in the database.");
			}
			Department department = mapperPoToDomain.mapDepartmentPoToDepartment(departmentPo);
			GetDepartmentOutput result = new GetDepartmentOutput();
			result.setDepartment(department);
			return Response.status(Response.Status.OK).entity(result).build();
		} catch (Exception e) {
			return handleException(GetDepartmentOutput.class, e, Response.Status.BAD_REQUEST);
		}
	}

}